<?php
/**
 * 文件柜分发系统
 * @author C.Jango
 * 实现站内用户的文件上传和下达功能
 */
namespace Admin\Controller;

class CabinetController extends AdminController {
	/**
	 * 我的文件柜
	 */
	function index() {
		$list = $this->lists(D('Cabinet'),array('uid'=>UID),'create_time desc');
		$this->assign('_list',$list);
		$this->meta_title = '我的文件';
		$this->display();
	}
	/**
	 * 增加文件
	 */
	function addin() {
		if (IS_POST) {
			$title = I('post.title');
			empty($title) && $this->error('请填写文件的标题！');
			$file = json_decode(think_decrypt(I('post.file_id')), true);
			!$file && $this->error('没有选择上传文件！');
			//将文件信息写入数据库
			$data['uid'] = UID;
			$data['title'] = $title;
			$data['category'] = I('post.category');
			$data['file_id'] = $file['id'];
			$data['create_time'] = NOW_TIME;
			if (D('Cabinet')->add($data)) {
				$this->success('添加文件成功！',U('index'));
			}else {
				$this->error('添加文件失败！');
			}
		}else {
			$category = M('CabinetCategory')->where('status=1')->select();
			$this->assign('category', $category);
			$this->meta_title = '新增文件';
			$this->display();
		}
	}
	/**
	 * 修改文件类别
	 */
	function change($id) {
		if (IS_POST) {
			$model = M('Cabinet');
			$data['id'] = $id;
			$data['category'] = I('post.category');
			if ($model->save($data)) {
				$this->success('修改分类成功！',U('index'));
			}else {
				$this->error('修改分类失败！');
			}
		}else {
			$info = M('Cabinet')->find($id);
			!$info && $this->error('文件不存在！');
			($info['uid'] != UID) && $this->error('非法操作！');
			$this->assign('info',$info);
			$category = M('CabinetCategory')->where('status=1')->select();
			$this->assign('category', $category);
			$this->meta_title = '修改类别';
			$this->display();
		}
	}
	/**
	 * 分发文件
	 */
	function distribute($id) {
		if (IS_POST) {
			$model = M('CabinetAccess');
			$cab_id = I('post.cab_id');
			foreach (I('post.to_uid') as $v) {
				$dataList[] = array('cab_id'=>$cab_id,'to_uid'=>$v,'pub_time'=>NOW_TIME);
			}
			if ($model->addAll($dataList)){
				$this->success('文件分发成功！',U('index'));
			}else {
				$this->error('文件分发失败，或已分发过此文件！');
			}
		}else {
			//提取文件信息
			$model = D('Cabinet');
			$info = $model->field('id,title')->find($id);
			$this->assign('info',$info);
			//展示用户列表
			$gList = M('Group')->where('gid<>'.C('NEW_USER_GROUP'))->select();
			$this->assign('_glist',$gList);
			$userList = M('member')->where('group_id<>'.C('NEW_USER_GROUP').' AND uid <> '.UID)->select();
			$this->assign('_user_list',$userList);
			//提取已经分发过的用户
			$recUser = M('CabinetAccess')->where('cab_id='.$id)->getField('to_uid',true);
			//将数组转换成字符串
			$recUser = arr2str($recUser);
			$this->assign('recUser',$recUser);
			
			$this->meta_title = '分发文件';
			$this->display();
		}
	}
	/**
	 * 删除我的文件,删除文件记录，但是不删除文件本身，以便他人转载
	 */
	function delmine() {
		$model = D('Cabinet');
		if (empty($_REQUEST['id'])) {
			$this->error('请选择至少一个文件记录！');
		}
		$ids = arr2str(I('request.id'));
		$map['uid'] = UID;
		$map['id'] = array('IN',$ids);
		if ($model->where($map)->delete()) {
			$this->success('删除文件成功！');
		}else {
			$this->error('删除文件失败！');
		}
	}
	/**
	 * 收到的文件
	 */
	function received() {
		$prefix = C('DB_PREFIX');
		$model = M('CabinetAccess')->alias('l')->join($prefix.'cabinet r ON l.cab_id=r.id');
		$list = $this->lists($model,array('to_uid'=>UID,'id'=>array('gt',0)),'create_time desc',null,'*');
		$this->assign('_list',$list);
		$this->meta_title = '收到的文件';
		$this->display();
	}
	/**
	 * 删除收到的文件记录
	 */
	function delrec() {
		$model = M('CabinetAccess');
		$ids = I('id');
		empty($ids) && $this->error('请选择至少一个文件记录！');
		$ids = arr2str((array)I('id'));
		$map['to_uid'] = UID;
		$map['cab_id'] = array('IN',$ids);
		if ($model->where($map)->delete()) {
			$this->success('删除文件成功！');
		}else {
			$this->error('删除文件失败！'.$model->getLastSql());
		}
	}
	/**
	 * 转储文件到我的文件中
	 */
	function transfer($id) {
		$model = D('Cabinet');
		$res = $model->find($id);
		!$res && $this->error('该文件已失效！');
		$data['fid'] = $res['id'];
		$data['fuid'] = $res['uid'];
		if ($model->where($data)->find()) {
			$this->error('已经转储过此文件，请勿重复操作！');
		}
		$data['uid']         = UID;
		$data['title']       = $res['title'];
		$data['category']    = $res['category'];
		$data['file_id']     = $res['file_id'];
		$data['create_time'] = NOW_TIME;
		if ($model->add($data)) {
			$this->success('转储文件成功！');
		}else {
			$this->error('转储文件失败！');
		}
	}
	/**
	 * 下载文件
	 */
	function download($id) {
		//判断文件是否存在
		$cabinet = D('Cabinet')->find($id);
		!$cabinet && $this->error('此文件不存在！');
		if ($cabinet['uid'] != UID) {
			//判断接受者是否是当前用户
			$map['cab_id'] = $id;
			$map['to_uid'] = UID;
			$res = M('cabinet_access')->where($map)->find();
			!$res && $this->error('无权下载该文件！');
			//标记已读，并记录首次下载时间
			if ($res['readed']!=1) {
				$cabinet_access = M('CabinetAccess');
				$access_data = array('readed'=>1,'read_time'=>NOW_TIME);
				$cabinet_access->where($map)->setField($access_data);
			}
		}
		//下载文件
		$File = D('File');
		$root = C('DOWNLOAD_UPLOAD.rootPath');
		//下载文件 并调用回调函数 使下载次数+1
		if ($cabinet['uid'] != UID) {
			$down = $File->download($root,$cabinet['file_id'],array($this,'addDownTime'),$id);
		}else {
			$down = $File->download($root,$cabinet['file_id']);
		}
		!$down && $this->error($File->getError());
	}
	/**
	 * 下载文件后，下载次数+1
	 */
	function addDownTime($id) {
		$map['id'] = $id;
		D('Cabinet')->where($map)->setInc('download');
	}
	/**
	 * 文件分类
	 */
	function category() {
		$lists = $this->lists('CabinetCategory');
		$this->assign('_list', $lists);
		$this->meta_title = '文件分类';
		$this->display();
	}
	/**
	 * 增加分类
	 */
	function add() {
		if (IS_POST) {
			$title = I('post.title');
			empty($title) && $this->error('分类名称不能为空！');
			$model = M('CabinetCategory');
			$data['title'] = I('post.title');
			$data['remark'] = I('post.remark');
			$data['status'] = 1;
			$data['create_time'] = NOW_TIME;
			if ($model->add($data)) {
				$this->success('添加分类成功！',U('category'));
			}else {
				$this->error('添加分类失败！');
			}
		}else {
			$this->meta_title = '增加分类';
			$this->display('edit');
		}
	}
	/**
	 * 编辑分类
	 */
	function edit($id) {
		if (IS_POST) {
			$title = I('post.title');
			empty($title) && $this->error('分类名称不能为空！');
			$model = M('CabinetCategory');
			$data['id']    = I('post.id');
			$data['title'] = I('post.title');
			$data['remark'] = I('post.remark');
			$data['update_time'] = NOW_TIME;
			if ($model->save($data)) {
				$this->success('编辑分类成功！',U('category'));
			}else {
				$this->error('编辑分类失败！');
			}
		}else {
			$info = M('CabinetCategory')->find($id);
			$this->assign('info',$info);
			$this->meta_title = '编辑分类';
			$this->display('edit');
		}
	}
	/**
	 * 禁用分类
	 */
	function forb($id) {
		$map['id'] = $id;
		$this->forbid('CabinetCategory',$map);
	}
	/**
	 * 启用分类
	 */
	function rest($id) {
		$map['id'] = $id;
		$this->resume('CabinetCategory',$map);
	}
	/**
	 * 删除分类
	 */
	function del() {
		$ids = I('id');
		empty($ids) && $this->error('请选择至少一条数据！');
		$map['id'] = array('IN',$ids);
		parent::delete('CabinetCategory',$map);
	}
	
	function publish($id) {
		$model = D('Cabinet');
		$file = $model->find($id);
		if ($file['uid'] != UID) $this->error('禁止操作他人的文件！');
		//验证文件格式
		$fileInfo = M('File')->find($file['file_id']);
		$info['title'] = $file['title'];
		$filePath = C('DOWNLOAD_UPLOAD.rootPath').$fileInfo['savepath'].$fileInfo['savename'];
		
		if ($fileInfo['ext'] == 'doc' || $fileInfo['ext'] == 'docx') {
			$info['content'] = $model->getDocContent($filePath);
		}elseif ($fileInfo['ext'] == 'jpg') {
			$info['content'] = $fileInfo;
		}else {
			$this->error('该文件类型 【】 不允许直接发布到内容！');
		}
		$this->assign('info',$info);
		$this->meta_title = '转发到内容';
		$this->display();
	}
}